<?php
include('rootlogin.php');
include "../Classes\PHPExcel.php";
//include('../incsession.php');
session_start();
$level=2;
//add to database and leave
if($_POST['submit']=="next"){
	// Export xls
	/** Error reporting */ 
	error_reporting(E_ALL); 	
	/** PHPExcel */ 
	require_once "../Classes\PHPExcel.php"; 	
	/** PHPExcel_IOFactory */ 
	require_once "../Classes\PHPExcel\IOFactory.php";
	$objPHPExcel = new PHPExcel(); 
	$objPHPExcel->setActiveSheetIndex(0);
	
	$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setSize(16);
	$objPHPExcel->getActiveSheet()->setCellValue("A1","題目敘述");
	$objPHPExcel->getActiveSheet()->getStyle("B1")->getFont()->setSize(16);
	$objPHPExcel->getActiveSheet()->setCellValue("B1","正確答案");
	
	$DND1=$_POST['DND1'];
	for($i=1; $i<=$DND1; $i++){
				$DND2[$i]=$_POST['DND2_'.$i];
				for($j=1; $j<=$DND2[$i]; $j++){
					$DND3[$i][$j]=$_POST['DND3_'.$i.'_'.$j];	
				}
	}
	$FAKE=$_POST['FAKE'];
	$i=1;
	$m=1;
	$excel_row=0;
	while($i <= $DND1)
	{	
		if($_POST['Q'.$i]!='' && $_POST['Q'.$i]!=' '){
			$layer=$m*10000;
			
			
			$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
			$A = str_replace('"', '&quot;', $A);
			$query = "INSERT INTO drag_and_drop (question, layer, topicid, level) 
				VALUES ('".$Q."',
							'".$layer."', 
							".$_SESSION['topicid'].", ".$level.")";
			$result = mysql_query($query,$user_con);
			$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
				VALUES ('".$A."',
							'".$layer."', 
							".$_SESSION['topicid'].", ".$level.")";
			$result = mysql_query($query,$user_con);
			//excel
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "ADDFFF")), ) );
			$Q = str_replace("&#39;", "'", $Q);
			$Q = str_replace('&quot;', '"', $Q);
			$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2)," ".$Q);
			
			$A = str_replace("&#39;", "'", $A);
			$A = str_replace('&quot;', '"', $A);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "e2e2e2")), ) );
			$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
			$excel_row++;
			//excel
			$n=1;
			$j=1;
			while($j <= $DND2[$i]){
				if($_POST["Q".$i."_".$j]!='' && $_POST["Q".$i."_".$j]!=' '){
					$layer=$m*10000+$n*100;
					$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
					$Q = str_replace('"', '&quot;', $Q);
					$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
					$A = str_replace('"', '&quot;', $A);
					$query = "INSERT INTO drag_and_drop (question, layer, topicid, level) 
								VALUES ('".$Q."',
								'".$layer."', 
								".$_SESSION['topicid'].", ".$level.")";
					$result = mysql_query($query,$user_con);
					$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
								VALUES ('".$A."',
								'".$layer."', 
								".$_SESSION['topicid'].", ".$level.")";
					$result = mysql_query($query,$user_con);
					
					//excel
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "BDEDFF")), ) );
					$Q = str_replace("&#39;", "'", $Q);
					$Q = str_replace('&quot;', '"', $Q);
					$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"     ".$Q);
			
					$A = str_replace("&#39;", "'", $A);
					$A = str_replace('&quot;', '"', $A);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "e2e2e2")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
					$excel_row++;
					//excel
					
					$p=1;
					$k=1;
					while($k <= $DND3[$i][$j]){
						if($_POST["Q".$i."_".$j."_".$k]!='' && $_POST["Q".$i."_".$j."_".$k]!=' '){
							$layer=$m*10000+$n*100+$p;
							$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
							$Q = str_replace('"', '&quot;', $Q);
							$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
							$A = str_replace('"', '&quot;', $A);
							$query = "INSERT INTO drag_and_drop (question, layer, topicid, level) 
									VALUES ('".$Q."',
									'".$layer."', 
									".$_SESSION['topicid'].", ".$level.")";
							$result = mysql_query($query,$user_con);
							$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
									VALUES ('".$A."',
									'".$layer."', 
									".$_SESSION['topicid'].", ".$level.")";
							$result = mysql_query($query,$user_con);
							
							//excel
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "E0FFFF")), ) );
							$Q = str_replace("&#39;", "'", $Q);
							$Q = str_replace('&quot;', '"', $Q);
							$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"           ".$Q);
			
							$A = str_replace("&#39;", "'", $A);
							$A = str_replace('&quot;', '"', $A);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "e2e2e2")), ) );
							$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
							$excel_row++;
							//excel
							
							$p++;
						}
						$k++;
					}
					$n++;
				}
				$j++;	
			}
			$m++;
		}
		$i++;
	}
	
	$sql = "SELECT name FROM `topic` WHERE `topicid` = '".$_SESSION['topicid']."'";
	$result = mysql_query($sql, $user_con);
	$tname = mysql_fetch_row($result);
	$filename = "../dnd_answer/".$tname[0]."_".$_SESSION['topicid']."_".$level.".xls";
	
	$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); 
	$objWriter->save($filename);
	
	$i=1;
	while($i<=$FAKE){
		if($_POST['fake'.$i]!='' && $_POST['fake'.$i]!=' '){
				$layer=0;
				$f_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
				$f_ans = str_replace('"', '&quot;', $f_ans);
				$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
							VALUES ('".$f_ans."',
							'".$layer."', 
							".$_SESSION['topicid'].", ".$level.")";
				$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	header("location: video_intro3.php");	
}
//value initialization
if(!isset($_POST['DND1'])){
			$DND1=1;
			$DND2[1]=0;
			$DND3[1][1]=0;
			$FAKE=1;
}
else{
	$DND1=$_POST['DND1'];
	for($i=1; $i<=$DND1; $i++){
				$DND2[$i]=$_POST['DND2_'.$i];
				for($j=1; $j<=$DND2[$i]; $j++){
					$DND3[$i][$j]=$_POST['DND3_'.$i.'_'.$j];	
				}
	}
	$FAKE=$_POST['FAKE'];
	if($_POST['submit']=="add a fake answer"){
		$FAKE++;
	}
	else if($_POST['submit']=="add layer 1"){
			$DND1=$_POST['DND1'];
			$DND1++;		
			$DND2[$DND1]=0;
			$DND3[$DND1][1]=0;
	}
	else{
		for($i=1; $i<=$DND1; $i++){
			$str="add layer 2 to Q".$i;
			if($_POST['submit']==$str){
				$DND2[$i]++;
				$DND3[$i][$DND2[$i]]=0;
				break;
			}
			for($j=1; $j<=$DND2[$i]; $j++){
				$str="add layer 3 to Q".$i."-".$j;
				if($_POST['submit']==$str){
					$DND3[$i][$j]++;
					break;
				}
			}
		}
	}
}
?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="framestyle.css" type="text/css" media="screen">
<style type="text/css">
</style>
<script>
</script>
</head>

<body>
<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">
<hr/>
<div id="title2">FOR INTERMEDIATE LEVEL: DRAG AND DROP</div>
<hr/><br><br>
<form method = POST action="drag_and_drop2.php" >



<?php
$i=1;

while($i<=$DND1){
	$j=1;
	while($j<=$DND2[$i]){
		echo '<input type="hidden" name="DND3_'.$i.'_'.$j.'" value='.$DND3[$i][$j].'>';
		$j++;
	}
	$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
	$A = str_replace('"', '&quot;', $A);
	echo '<div class="layer first">
	T'.$i.': <input class="area long" type="text" name="Q'.$i.'" value="'.$Q.'"><br>
	answer: <input class="area long" type="text" name="answer'.$i.'" value="'.$A.'"><br>
	<input type="hidden" name="DND1" value='.$DND1.'>
	<input type="hidden" name="DND2_'.$i.'" value='.$DND2[$i].'>
	<input type="submit" name = "submit" value = "add layer 2 to Q'.$i.'"></div>';
	$j=1;
	while($j<=$DND2[$i]){
		$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
		$A = str_replace('"', '&quot;', $A);
		echo '<div class="layer second">
			T'.$i.'-'.$j.' : <input class="area medium" type="text" name="Q'.$i.'_'.$j.'" value="'.$Q.'"><br>
			answer: <input class="area medium" type="text" name="answer'.$i.'_'.$j.'" value="'.$A.'"><br>
			<input type="submit" name = "submit" value = "add layer 3 to Q'.$i.'-'.$j.'"></div>';
		$k=1;
		while($k<=$DND3[$i][$j]){
			$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
			$A = str_replace('"', '&quot;', $A);
			echo '<div class="layer third">
				T'.$i.'-'.$j.'-'.$k.': <input class="area short" type="text" name="Q'.$i.'_'.$j.'_'.$k.'" 
				value="'.$Q.'"><br>
				answer: <input class="area short" type="text" name="answer'.$i.'_'.$j.'_'.$k.'" value="'.$A.'"><br><br></div>';
			$k++;
		}
		$j++;	
	}	
	
	$i++;
	echo "<br><br>";
}
echo '<div class="btn_right"><input type="submit" name = "submit" value = "add layer 1"></div><br><br><br>';
$i=1;
while($i<=$FAKE){
	$f_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
	$f_ans = str_replace('"', '&quot;', $f_ans);
	echo "<div class='layer fake'>";
	echo 'fake ans: <td><input class="area long" type="text" name="fake'.$i.'" value="'.$f_ans.'">';	
	echo "</div><br>";
	$i++;
}
echo '<div class="btn_right"><input type="submit" name = "submit" value = "add a fake answer"></div>';
echo "<input type='hidden' name='FAKE' value=".$FAKE.">";


?>


<br><br>
<div class="btn_right"><input type="submit" name = "submit" value = "next"></div>
</div>
</form>




</body>

